package org.wk.concurrent.app1;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.Callable;

public class Crawler implements Callable<File> {
	private String url = null;
	public Crawler(String url) {
		this.url = url;
	}

	/**
	 * 传入一个url，通过这个url下载页面，返回一个File对象。
	 * @param url
	 * @return
	 * @throws IOException 
	 */
	public File fetch(String url) throws IOException {
		URL fetchUrl = null;
		HttpURLConnection conn = null;
		BufferedReader reader = null;
		PrintWriter writer = null;
		File file = new File(getFilePath(url));
		try {
			fetchUrl = new URL(url);
			conn = (HttpURLConnection) fetchUrl.openConnection();
			reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
			writer = new PrintWriter(file.getAbsoluteFile(), "GBK");
			
			String line = "";
			while((line = reader.readLine()) != null) {
				writer.println(line);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			reader.close();
			writer.close();
		}
		return file;
	}
	
	/**
	 * 通过传入url获得文件的存储路径
	 * @param url
	 * @return
	 */
	protected String getFilePath(String url) {
		StringBuilder builder = new StringBuilder();
		builder.append("/").append(System.nanoTime()).append(".txt");
		return builder.toString();
	}
	
	@Override
	public File call() throws Exception {
		return fetch(url);
	}
}	
